package cpr;

import java.awt.Color;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
/**
 * 
 * @author Christopher Ramelb
 * 
 * This Robot is adapted from the sample robot Walls and hopes to kick butt.
 *
 * BobBot was adapted from Robocode sample robot "Walls" with some improvements. 
 * I notice that Walls sometimes traveled the longest route to get to the initial wall.
 * I wanted to improve this code by turning my robot to the closest wall.  
 */
public class BobBot extends Robot {
  
  //turnCounter counts the number of turns
  //int turnCounter = 0;
  
  
  /**
   * This is the is the "main()" in robocode.
   */
  public void run() {
    
    //customize colors
    setColors(Color.red, Color.green, Color.yellow);
    setBulletColor(Color.black);
    setScanColor(Color.white);
    
    
    // Initialize moveAmount to the maximum possible for this battlefield.
    // Taken from Walls sample robot, I subtracted some pixels so my robot
    // could still defend against a back attack but also be closer and thus
    // more accurate with his firing.
    double moveAmount = Math.max(getBattleFieldWidth() - 80, getBattleFieldHeight() - 80);
   
    //halfHeight is half the battlefields height
    double halfHeight = getBattleFieldHeight() / 2;
    //curentHeight is your y coordinate position
    double currentHeight = getY();
    this.setDebugProperty("battleFieldHeight", String.valueOf(getBattleFieldHeight()));
    
    //if robot is facing Northeast and is in the top half of battlefield
    if ((getHeading() > 0 && getHeading() < 90) && (currentHeight > halfHeight)) {
      turnLeft(getHeading() % 90);
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }

    //if robot is facing Northeast and is in the bottom half of battlefield
    else if ((getHeading() > 0 && getHeading() < 90) && (currentHeight < halfHeight)) {
      turnRight(90 % getHeading());
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }

    //if robot is facing Southeast and is in the top half of battlefield
    else if ((getHeading() > 90 && getHeading() < 180) && (currentHeight > halfHeight)) {
      turnLeft(getHeading() % 180);
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }
    
    //if robot is facing Southeast and is in the bottom half of battlefield
    else if ((getHeading() > 90 && getHeading() < 180) && (currentHeight < halfHeight)) {
      turnRight(180 % getHeading());
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }

    //if robot is facing Southwest and is in the top half of battlefield
    else if ((getHeading() > 180 && getHeading() < 270) && (currentHeight > halfHeight)) {
      turnRight(270 % getHeading());
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }
    
    //if robot is facing Southwest and is in the bottom half of battlefield
    else if ((getHeading() > 180 && getHeading() < 270) && (currentHeight < halfHeight)) {
      turnLeft(getHeading() - 180);
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }
    //if robot is facing Northwest and is in the top half of battlefield
    else if ((getHeading() > 270 && getHeading() < 360) && (currentHeight > halfHeight)) {
      turnRight(360 % getHeading());
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }
    //if robot is facing Northwest and is in the bottom half of battlefield
    else if ((getHeading() > 270 && getHeading() < 360) && (currentHeight < halfHeight)) {
      turnLeft(getHeading() - 180);
      ahead(moveAmount);
      turnGunRight(90);
      turnRight(90);
    }

    while (true) {
      //This part of the code is called when a robot hits the corner
      //By turning right 90 degrees it scans the entire battlefield 
      ahead(moveAmount);
      turnRight(90);
      this.setDebugProperty("moveAmount", String.valueOf(moveAmount));
    }
  }
/**
 * When the Robot scans a robot firepower should be directly related to amount of remaining energy.
 * @param e is the scanned robot
 */
  public void onScannedRobot(ScannedRobotEvent e) {
    String firePower = "3";
    this.setDebugProperty("firePower", String.valueOf(firePower));
    
    if ((getEnergy() == 100) || (getEnergy() > 50)) {
      //absoluteBearing and the turnGunRight was found on robowiki at
      //http://robowiki.net/wiki/Head-On_Targeting I copied them directly.
      double absoluteBearing = getHeading() + e.getBearingRadians();
      turnGunRight(
          robocode.util.Utils.normalRelativeAngle(absoluteBearing - 
              getGunHeading()));
      fire(3);
    }
    else if (getEnergy() >= 50) {
      //absoluteBearing and the turnGunRight was found on robowiki at
      //http://robowiki.net/wiki/Head-On_Targeting I copied them directly.
      double absoluteBearing = getHeading() + e.getBearingRadians();
      turnGunRight(
          robocode.util.Utils.normalRelativeAngle(absoluteBearing - 
              getGunHeading()));
      fire(2.25);
    }
    else if (getEnergy() < 50) {
      //absoluteBearing and the turnGunRight was found on robowiki at
      //http://robowiki.net/wiki/Head-On_Targeting I copied them directly.
      double absoluteBearing = getHeading() + e.getBearingRadians();
      turnGunRight(
          robocode.util.Utils.normalRelativeAngle(absoluteBearing - 
              getGunHeading()));
      fire(1.85);
    }
    else if (getEnergy() <= 25 && getEnergy() > 10) {
      //absoluteBearing and the turnGunRight was found on robowiki at
      //http://robowiki.net/wiki/Head-On_Targeting I copied them directly.
      double absoluteBearing = getHeading() + e.getBearingRadians();
      turnGunRight(
          robocode.util.Utils.normalRelativeAngle(absoluteBearing - 
              getGunHeading()));
      fire(1.5);
    }
    else if (getEnergy() <= 10) {
      //absoluteBearing and the turnGunRight was found on robowiki at
      //http://robowiki.net/wiki/Head-On_Targeting I copied them directly.
      double absoluteBearing = getHeading() + e.getBearingRadians();
      turnGunRight(
          robocode.util.Utils.normalRelativeAngle(absoluteBearing - 
              getGunHeading()));
      fire(1);
    }
  }
  
  /**
   * Will move slightly away from a robot when hit.
   * Taken from sample.Walls from the robocode sample package.
   * @param e is the robot that was hit.
   */
  public void onHitRobot(HitRobotEvent e) {
    // If he's in front of us, set back up a bit.
    if (e.getBearing() > -90 && e.getBearing() < 90) {
      back(100);
    } // else he's in back of us, so set ahead a bit.
    else {
      ahead(100);
    }
  }
}


  /*
  public void onBulletHit(BulletHitEvent h){
    while(true) {
    count++;
    if(count > 10) {
      ahead(40);
      turnRight(30);
      ahead(40);
      turnLeft(30);
    }
    }
  }
}
*/